﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class UIEKP : ViewBase
    {
        public bool IsOpen
        {
            get
            {
                return ((VEKPOS)MV).IsOpen;
            }
            set
            {
                ((VEKPOS)MV).IsOpen = value;
            }
        }
        public UIEKP()
        {
            MESSAGES += "EKPDEL;"; 
        }
        /// <summary>
        /// SetProd 当前订单
        /// SetData保存所有订单，但是用于展示今日订单
        /// SetEnv保存所有订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
            if (!TBMain.Columns.Contains("PART"))
            {
                TBMain.Columns.Add("PART");
                TBMain.Columns.Add("PVAR");
                TBMain.Columns.Add("PFARBE");
            }
            FTAUTOSUM = true;
            TCOUNT1 = "行数";
            TKEY1 = "数量";
            SKEY1 = "EKPSA";
            KEY1 = "EKPME1";
            TKEY2 = "本币";
            SKEY2 = "EKPSA";
            KEY2 = "EKPWE1";
            TKEY3 = "交易";
            SKEY3 = "EKPSA";
            KEY3 = "BUVALUE";
        }
        public override void OnWorkflowIDChange(ReflectionStudio.Core.Events.MessageEventArgs e)
        {
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            {
                case "EKPDEL":
                    EKPDEL(msg);
                    break;
            }
        }


        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            string _date = "", _time = "";
            Helper.GetTime(ref _date, ref _time);

            dwData = TBMain.NewRow();
            ((VEKPOS)MV).SetUpNewRow(ref dwData, null);
            dwData["EKPEDIZEIT"] = _time.Substring(0, 4);
            dwData["EKPSA"] = "PO";
            dwData["EKPAUFKRS"] = KRS;
            dwData["EKPAUFLFD"] = LFD;
            LINE++;
            dwData["EKPAUFPOS"] = Helpers.Helper.Right("000" + LINE, 3);
            dwData["EKPLIEFNR"] = LIEFNR;
            dwData["EKPMEEH"] = MEEH.PadRight(3);
            dwData["EKPKURS"] = KURS;
            INSERT(dwData);
            base.NewCommand(sender, e);
        }
        public override void OnEXAUFChg(string value)
        {
            TBMain.Clear();
            if (value.Length < 19)
                return;
            KRS = value.Substring(1, 2);
            LFD = value.Substring(3, 6);
            LIEFNR = value.Substring(12, 8);
            LINE = 0;
            dwData = TBMain.NewRow();
            dwData["EKPMANDANT"] = app.Mandant;
            dwData["EKPSA"] = "PO";
            dwData["EKPAUFKRS"] = KRS;
            dwData["EKPAUFLFD"] = LFD;
            SResult = ((VEKPOS)MV).QEK02(dwData);
            MergeData(SResult, true);
            if (IsAuto)
            {
                GetPOS();
                if (TBMain.Rows.Count == 0)
                    NewCommand(null, null);
            }
            base.OnEXAUFChg(value);

        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwEKP == null)
                return;
            ((VEKPOS)MV).DeleteEKPOS(dwEKP, 1);
            base.DeleteCommand(sender, e);
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwEKP = dwMaster;
            IsOpen = true;
            IsInactive = false;
            if (dwMaster == null)
            {
                KEY = "";
                MEEH = "".PadRight(3);
                KURS = "0";
                TXTISIND = "";
                AUF = "";
                TMV.EKPDIFF = 0;
                IsInactive = false;
                LSLIEFNR = "";
            }
            else
            {
                if (TMV.EKPSA == "IN")
                {
                    IsInactive = true;
                }
                MEEH = TMV.EKPMEEH;
                KURS = (TMV.EKPKURS).ToString();

                if (((VEKPOS)MV).EKPFILLER14.Substring(0, 1) != "0")
                {
                    IsOpen = false;
                }
                TXTISIND = (TMV.EKPAUFKRS + TMV.EKPAUFLFD + TMV.EKPAUFPOS).PadRight(60);
                EXARTICLE = TMV.EKPWAKZ + (TMV.PART + TMV.PVAR).Trim().PadRight(22) + TMV.PFARBE;
                TMV.EKPDIFF = TMV.EKPME1 - TMV.EKPME3;
                LSLIEFNR = TMV.EKPLIEFNR;
                if (IsInactive)
                    return;
                KEY = TMV.EKPAUFKRS + TMV.EKPAUFLFD + TMV.EKPAUFPOS + "B";
                AUF = "B" + TMV.EKPAUFKRS + TMV.EKPAUFLFD + TMV.EKPAUFPOS + LIEFNR.PadRight(8);

            }
        }
        public override void Copy()
        {
            base.Copy();
            if (dwEKP == null)
                return;
            DataRow dwData;
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwEKP.ItemArray;
            LINE++;
            dwData["EKPAUFPOS"] = Helpers.Helper.Right("000" + LINE, 3);
            dwData["EKPME1"] = 0;
            dwData["EKPWE1"] = 0;
            INSERT(dwData);
        }
        public override void OnSave()
        {
            if (!IsSaved)
                return;
            if (TMV.EKPLTERM > 0 && TMV.EKPLTERM.ToString().Trim().Length < 8)
            {
                MessageBox.Show("日期格式为YYYYMMDD如（20150101），请修正！");
                return;
            }
            if (TMV.EKPGENDAT1 > 0 && TMV.EKPGENDAT1.ToString().Trim().Length < 8)
            {
                MessageBox.Show("日期格式为YYYYMMDD如（20150101），请修正！");
                return;
            }
            if (TMV.EKPWUTERM > 0 && TMV.EKPWUTERM.ToString().Trim().Length < 8)
            {
                MessageBox.Show("日期格式为YYYYMMDD如（20150101），请修正！");
                return;
            }
            if (!IsSaved)
                return;
            foreach (DataRow row in TBMain.Rows)
            {
                UPDATEEKP(row);
            }
            PM["KRS"] = dwEKP["EKPAUFKRS"].ToString();
            PM["LFD"] = dwEKP["EKPAUFLFD"].ToString();
            TMV.REMOVE0QTY(PM);
            base.OnSave();
        }
        public double LINE = 0;

        public void GetPOS()
        {
            var qData = from XD in TBMain.AsEnumerable()   
                        where XD["EKPAUFKRS"].ToString()==KRS
                        & XD["EKPAUFLFD"].ToString() ==LFD
                        group XD by new
                        {
                            KRS = XD["EKPAUFKRS"].ToString(),
                            LFD = XD["EKPAUFLFD"].ToString(),
                        }
                            into g
                        select new
                        {
                            KEY = g.Key,
                            LPOS = g.Max(XD => Convert.ToDouble(XD["EKPAUFPOS"].ToString())),
                        };
            foreach (var XD in qData)
            {
                LINE = XD.LPOS;
            }
        }

        public void INSERT(DataRow _dwEKP)
        {
            ((VEKPOS)MV).Insert(_dwEKP);
            vwMaster.View.MoveCurrentToFirst();
        }

        public void EKPDEL(MessageInfo msg)
        {
            DataRow dwData = TBMain.NewRow();
            dwData["EKPMANDANT"] = app.Mandant;
            dwData["EKPSA"] = "PO";
            dwData["EKPAUFKRS"] = msg.KRS;
            dwData["EKPAUFLFD"] = msg.LFD;
            dwData["EKPAUFPOS"] = msg.POS;
            ((VEKPOS)MV).DeleteEKPOS(dwData, 1);
        }
        public void DT2ALLCommand(object sender, RoutedEventArgs e)
        {
            if (TMV.EKPLTERM > 0 && TMV.EKPLTERM.ToString().Trim().Length < 8)
            {
                MessageBox.Show("日期格式为YYYYMMDD如（20150101），请修正！");
                return;
            }
            if (TMV.EKPGENDAT1 > 0 && TMV.EKPGENDAT1.ToString().Trim().Length < 8)
            {
                MessageBox.Show("日期格式为YYYYMMDD如（20150101），请修正！");
                return;
            }
            if (TMV.EKPWUTERM > 0 && TMV.EKPWUTERM.ToString().Trim().Length < 8)
            {
                MessageBox.Show("日期格式为YYYYMMDD如（20150101），请修正！");
                return;
            }
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            foreach (DataRow row in TBMain.Rows)
            {
                row["EKPLTERM"] = dwMaster["EKPLTERM"];
            }

        }

        public void SP2ALLCommand(object sender, RoutedEventArgs e)
        {
            if (TMV.EKPLTERM > 0 && TMV.EKPLTERM.ToString().Trim().Length < 8)
            {
                MessageBox.Show("日期格式为YYYYMMDD如（20150101），请修正！");
                return;
            }
            if (TMV.EKPGENDAT1 > 0 && TMV.EKPGENDAT1.ToString().Trim().Length < 8)
            {
                MessageBox.Show("日期格式为YYYYMMDD如（20150101），请修正！");
                return;
            }
            if (TMV.EKPWUTERM > 0 && TMV.EKPWUTERM.ToString().Trim().Length < 8)
            {
                MessageBox.Show("日期格式为YYYYMMDD如（20150101），请修正！");
                return;
            }
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            foreach (DataRow row in TBMain.Rows)
            {
                row["EKPGENDAT1"] = dwMaster["EKPGENDAT1"];
            }
        }
        public void WU2ALLCommand(object sender, RoutedEventArgs e)
        {
            if (TMV.EKPLTERM > 0 && TMV.EKPLTERM.ToString().Trim().Length < 8)
            {
                MessageBox.Show("日期格式为YYYYMMDD如（20150101），请修正！");
                return;
            }
            if (TMV.EKPGENDAT1 > 0 && TMV.EKPGENDAT1.ToString().Trim().Length < 8)
            {
                MessageBox.Show("日期格式为YYYYMMDD如（20150101），请修正！");
                return;
            }
            if (TMV.EKPWUTERM > 0 && TMV.EKPWUTERM.ToString().Trim().Length < 8)
            {
                MessageBox.Show("日期格式为YYYYMMDD如（20150101），请修正！");
                return;
            }
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            foreach (DataRow row in TBMain.Rows)
            {
                row["EKPWUTERM"] = dwMaster["EKPWUTERM"];
            }
        }
        public bool Validate()
        {
            bool IsOK = true;
            try
            {
                if (TxtPrice != null)
                {
                    TxtPrice.Focus();
                }
                TxtQty.Focus();
                if (dwEKP["EKPLTERM"].ToString().Trim().Length == 0)
                {
                    MessageBox.Show("请先输入交期");
                    return false;
                }
                if (dwEKP["EKPART"].ToString().Trim().Length == 0
                || dwEKP["EKPFARBE"].ToString().Trim().Length == 0)
                {
                    MessageBox.Show("请先输入品种或色号");
                    return false;
                }
                if (dwEKP["EKPFARBE"].ToString().Trim().Length == 0)
                {
                    MessageBox.Show("请先输入色号");
                    return false;
                }
                if (IsPrice)
                {
                    if (Convert.ToDouble(dwEKP["EKPPRME"].ToString()) * 0.001 == 0)
                    {
                        MessageBox.Show("请先输入价格");
                        return false;
                    }
                    if (MEEH.Trim().Length == 0)
                    {
                        MessageBox.Show("请先输入单位");
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return IsOK;
        }

        public void UPDATEEKP(DataRow _dwEKP)
        {
            ((VEKPOS)MV).UPDATEEKP(_dwEKP);//计算价值
            ((VEKPOS)MV).UpdateEKPOS(_dwEKP, 0);
            PM["KRS"] = dwEKP["EKPAUFKRS"].ToString();
            PM["LFD"] = dwEKP["EKPAUFLFD"].ToString();
            ((VEKPOS)MV).VEK.EKKCHK(PM);

        }

        public virtual void QTY_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {

            if (!IsEdit)
                return;

            if (e.Key != System.Windows.Input.Key.Enter)
                return;
            if (e.Key != System.Windows.Input.Key.Return)
                return;
            if (!Validate())
            {
                return;
            }

            UPDATEEKP(dwMaster);
            Copy();
        }

        public override void OnAppModeChg()
        {
            base.OnAppModeChg();
            switch (app.company)
            {
                case "TST":
                    switch (AppMode)
                    {
                        case "TISSUE":
                            // KRS = MV.year.Substring(3, 1) + "S";

                            break;
                        case "TMODE":
                            // KRS = MV.year.Substring(3, 1) + "M";
                            break;
                    }
                    break;

            }
        }

        public void EKWB(object sender, RoutedEventArgs e)
        {
            VICC_WFEVENTS.SVWF.EKWB(RUNID, KEY, LO, LB, LP);
        }

        public override void LOADBYAUF()
        {
            TBMain.Clear();
            if (ORDERNUM.Length < 11)
                return;
            dwData = TBMain.NewRow();
            dwData["EKPMANDANT"] = app.Mandant;
            dwData["EKPSA"] = "PO";
            dwData["EKPAUFKRS"] = ORDERNUM.Substring(0, 2);
            dwData["EKPAUFLFD"] = ORDERNUM.Substring(2, 6);
            dwData["EKPAUFPOS"] = ORDERNUM.Substring(8, 3);
            SResult = ((VEKPOS)MV).QueryEKPOS(dwData, 8);
            MergeData(SResult, true);
            base.LOADBYAUF();
        }

        public override void InactiveCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            dwMaster["EKPSA"] = "IN";
            dwData = TBMain.NewRow();
            dwData["EKPMANDANT"] = app.Mandant;
            dwData["EKPSA"] = "IN";
            dwData["EKPAUFKRS"] = dwMaster["EKPAUFKRS"];
            dwData["EKPAUFLFD"] = dwMaster["EKPAUFLFD"];
            dwData["EKPAUFPOS"] = " AND EKPAUFPOS='" + dwMaster["EKPAUFPOS"].ToString() + "'";
            TMV.UpdateEKPOS(dwData, 20);

            dwData = TMV.VZB.TBMain.NewRow();
            dwData["VKZMANDANT"] = app.Mandant;
            dwData["VKZSA"] = "EK";
            dwData["VKZAUFKRS"] = dwMaster["EKPAUFKRS"];
            dwData["VKZAUFLFD"] = dwMaster["EKPAUFLFD"];
            dwData["VKZAUFPOS"] = " AND VKZAUFPOS='" + dwMaster["EKPAUFPOS"].ToString() + "'";
            TMV.VZB.UpdateVKZUAB(dwData, app.company, 20);// 无任何该动

            dwData = TMV.MUX.TBMain.NewRow();
            dwData["ABAMANDANT"] = app.Mandant;
            dwData["ABAAUFTYP"] = "B";
            dwData["ABAAUFKRS"] = dwMaster["EKPAUFKRS"];
            dwData["ABAAUFLFD"] = dwMaster["EKPAUFLFD"];
            dwData["ABAAUFPOS"] = " AND ABAAUFPOS='" + dwMaster["EKPAUFPOS"].ToString() + "'";
            TMV.MUX.DeleteABAUFM(dwData, 21);

            dwData = TMV.VKD.TBMain.NewRow();
            dwData["VKAMANDANT"] = app.Mandant;
            dwData["VKAAUFKRS"] = dwMaster["EKPAUFKRS"];
            dwData["VKAAUFLFD"] = dwMaster["EKPAUFLFD"];
            dwData["VKAAUFPOS"] = " AND VKAAUFPOS='" + dwMaster["EKPAUFPOS"].ToString() + "'";

            dwData["VKASA"] = "EK";
            TMV.VKD.DeleteVKADR(dwData, 20);

            dwData = TMV.VWD.TBMain.NewRow();
            dwData["WDMANDANT"] = app.Mandant;
            dwData["WDAUFKRS"] = dwMaster["EKPAUFKRS"];
            dwData["WDAUFLFD"] = dwMaster["EKPAUFLFD"];
            dwData["WDAUFPOS"] = " AND WDAUFPOS='" + dwMaster["EKPAUFPOS"].ToString() + "'";
            dwData["WDSA"] = "B";
            TMV.VWD.DeleteWDECK(dwData, 20);

            PM["KRS"] = dwMaster["EKPAUFKRS"].ToString();
            PM["LFD"] = dwMaster["EKPAUFLFD"].ToString();
            TMV.VEK.EKPCHG(PM);
            base.InactiveCommand(sender, e);
        }
        public virtual void OnWSChg(DataRow ws)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            if (ws == null)
                return;

            TMV.EKPLIEFNR = ws["WALIEFNR"].ToString();
            TMV.EKPEART = ws["WALOHNA"].ToString();
            TMV.EKPEFARBE = Helper.Right(ws["WALOHNA"].ToString(), 10);
            string str = ws["WABIND"].ToString();
            if (str.Length > 0)
            {
                try
                {
                    TMV.EKPPRME = Helper.Right("0000000000" + (System.Convert.ToDouble(str.Substring(0, 8)) * 1000).ToString(), 10); ;//单价 
                    TMV.EKPMEEH = ws["WABIND"].ToString().Substring(12, 3); //单位  
                }
                catch (Exception ex)
                {
                    Tracer.Error(ex.StackTrace, ex);
                }
            }
            if (dwMain == null)
            { }
        }
        /*
        public virtual void CheckPrice(DataRow dwData)
        {
        switch (app.company)
        {
        case "TST":
        CHKTSTPRICE(dwData);
        break;
        }
        }

        public virtual void CheckDelDate(DataRow dwData)
        {
        dwData["VKPLIEFTERM"] = Helper.GetDateAddDays(MV.date, 30);
        }

        public void CHKTSTPRICE(DataRow dwData)
        {

        if (dwVF != null)
        {
        string PR = dwVF["VAAARTWEB"].ToString();
        string CURRENCY = PR.Substring(8, 3);
        string PRMEEH = PR.Substring(12, 3);
        string WSPRICE = PR.Substring(0, 8);
        if (WSPRICE.Trim() == "")
        {
        return;
        }
        dwData["VKPPREIS"] = Helper.Right("0000000000" + (System.Convert.ToDouble(WSPRICE) * 1000).ToString(), 10);
        dwData["VKPMEEH"] = PRMEEH;
        MEEH = "";
        MEEH = PRMEEH;
        //dwData["VKPPREIS"] = 
        }
        }
        */
    }
    public partial class UIEKP : ViewBase
    {
        public VEKPOS TMV
        {
            get
            {
                return (VEKPOS)MV;
            }
        }
        #region ==IsShowEKGB===========
        public static readonly DependencyProperty IsShowEKGBProperty = DependencyProperty.Register("IsShowEKGB",
        typeof(bool), typeof(UIEKP),
        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsShowEKGBChanged));
        public bool IsShowEKGB
        {
            get
            {
                return (bool)GetValue(IsShowEKGBProperty);
            }
            set
            {
                SetValue(IsShowEKGBProperty, value);
            }
        }
        private static void OnIsShowEKGBChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIEKP;
            if (src != null)
            {
                src.IsShowEKGB = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==IsShowLS===========
        public static readonly DependencyProperty IsShowLSProperty = DependencyProperty.Register("IsShowLS",
        typeof(bool), typeof(UIEKP),
        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsShowLSChanged));
        public bool IsShowLS
        {
            get
            {
                return (bool)GetValue(IsShowLSProperty);
            }
            set
            {
                SetValue(IsShowLSProperty, value);
            }
        }
        private static void OnIsShowLSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIEKP;
            if (src != null)
            {
                src.IsShowLS = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==FTAUTOEK===========
        public static readonly DependencyProperty FTAUTOEKProperty = DependencyProperty.Register("FTAUTOEK",
        typeof(bool), typeof(UIEKP),
        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFTAUTOEKChanged));
        public bool FTAUTOEK
        {
            get
            {
                return (bool)GetValue(FTAUTOEKProperty);
            }
            set
            {
                SetValue(FTAUTOEKProperty, value);
            }
        }
        private static void OnFTAUTOEKChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIEKP;
            if (src != null)
            {
                src.FTAUTOEK = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==IsPrice===========
        public static readonly DependencyProperty IsPriceProperty = DependencyProperty.Register("IsPrice",
        typeof(bool), typeof(UIEKP),
        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsPriceChanged));
        public bool IsPrice
        {
            get
            {
                return (bool)GetValue(IsPriceProperty);
            }
            set
            {
                SetValue(IsPriceProperty, value);
            }
        }
        private static void OnIsPriceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIEKP;
            if (src != null)
            {
                src.IsPrice = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==MEEH===========
        public static readonly DependencyProperty MEEHProperty = DependencyProperty.Register("MEEH",
        typeof(string), typeof(UIEKP),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnMEEHChanged));
        public string MEEH
        {
            get
            {
                return (string)GetValue(MEEHProperty);
            }
            set
            {
                SetValue(MEEHProperty, value);
                if (IsEdit && dwMaster != null)
                {
                    dwEKP["EKPMEEH"] = MEEH;
                }
            }
        }
        private static void OnMEEHChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIEKP;
            if (src != null)
            {
                src.MEEH = (string)e.NewValue;
            }
        }
        #endregion

        #region ==ARTST===========
        public static readonly DependencyProperty ARTSTProperty = DependencyProperty.Register("ARTST",
        typeof(string), typeof(UIEKP),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnARTSTChanged));
        public string ARTST
        {
            get
            {
                return (string)GetValue(ARTSTProperty);
            }
            set
            {
                SetValue(ARTSTProperty, value);
            }
        }
        private static void OnARTSTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIEKP;
            if (src != null)
            {
                src.ARTST = (string)e.NewValue;
            }
        }
        #endregion

        #region ==dwEKP===========
        public static readonly DependencyProperty dwEKPProperty = DependencyProperty.Register("dwEKP",
        typeof(DataRow), typeof(UIEKP),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwEKPChanged));
        public DataRow dwEKP
        {
            get
            {
                return (DataRow)GetValue(dwEKPProperty);
            }
            set
            {
                SetValue(dwEKPProperty, value);
            }
        }
        private static void OdwEKPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIEKP;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwEKP = row;
            }
        }
        #endregion

        #region ==dwEKK===========
        public static readonly DependencyProperty dwEKKProperty = DependencyProperty.Register("dwEKK",
        typeof(DataRow), typeof(UIEKP),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwEKKChanged));
        public DataRow dwEKK
        {
            get
            {
                return (DataRow)GetValue(dwEKKProperty);
            }
            set
            {
                SetValue(dwEKKProperty, value);
            }
        }
        private static void OndwEKKChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIEKP;
            DataRow row = (DataRow)e.NewValue;
            if (src != null)
            {
                src.dwEKK = row;
            }
        }
        #endregion

        #region ==AUF===========
        public static readonly DependencyProperty AUFProperty = DependencyProperty.Register("AUF",
        typeof(string), typeof(UIEKP),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnAUFChanged));
        public string AUF
        {
            get
            {
                return (string)GetValue(AUFProperty);
            }
            set
            {
                SetValue(AUFProperty, value);
            }
        }
        private static void OnAUFChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIEKP;
            if (src != null)
            {
                src.AUF = (string)e.NewValue;
            }
        }
        #endregion

        #region ==EXARTICLE===========
        public static readonly DependencyProperty EXARTICLEProperty = DependencyProperty.Register("EXARTICLE",
        typeof(string), typeof(UIEKP),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnEXARTICLEChanged));
        public string EXARTICLE
        {
            get
            {
                return (string)GetValue(EXARTICLEProperty);
            }
            set
            {
                SetValue(EXARTICLEProperty, value);
            }
        }
        private static void OnEXARTICLEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIEKP;
            if (src != null)
            {
                src.EXARTICLE = (string)e.NewValue;
            }
        }
        #endregion 

        #region ==LIEFNR===========
        public static readonly DependencyProperty LIEFNRProperty = DependencyProperty.Register("LIEFNR",
        typeof(string), typeof(UIEKP),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnLIEFNRChanged));
        public string LIEFNR
        {
            get
            {
                return (string)GetValue(LIEFNRProperty);
            }
            set
            {
                SetValue(LIEFNRProperty, value);
            }
        }
        private static void OnLIEFNRChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIEKP;
            if (src != null)
            {
                src.LIEFNR = (string)e.NewValue;
            }
        }
        #endregion
        #region ==AUFART===========
        public static readonly DependencyProperty AUFARTProperty = DependencyProperty.Register("AUFART",
        typeof(string), typeof(UIEKP),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnAUFARTChanged));
        public string AUFART
        {
            get
            {
                return (string)GetValue(AUFARTProperty);
            }
            set
            {
                SetValue(AUFARTProperty, value);

            }
        }
        private static void OnAUFARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIEKP;
            if (src != null)
            {
                src.AUFART = (string)e.NewValue;
            }
        }
        #endregion

        #region ==LSLIEFNR===========
        public static readonly DependencyProperty LSLIEFNRProperty = DependencyProperty.Register("LSLIEFNR",
        typeof(string), typeof(UIEKP),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnLSLIEFNRChanged));
        public string LSLIEFNR
        {
            get
            {
                return (string)GetValue(LSLIEFNRProperty);
            }
            set
            {
                SetValue(LSLIEFNRProperty, value);
            }
        }
        private static void OnLSLIEFNRChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIEKP;
            if (src != null)
            {
                src.LSLIEFNR = (string)e.NewValue;
            }
        }
        #endregion

        #region ==dwVF===========
        public static readonly DependencyProperty dwVFProperty = DependencyProperty.Register("dwVF",
        typeof(DataRow), typeof(UIEKP),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwVFChanged));
        public DataRow dwVF
        {
            get
            {
                return (DataRow)GetValue(dwVFProperty);
            }
            set
            {
                SetValue(dwVFProperty, value);
                OnVAFChg(value);
            }
        }
        private static void OndwVFChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIEKP;
            if (src != null)
            {
                src.dwVF = (DataRow)e.NewValue;
            }
        }
      
        public virtual void OnVAFChg(DataRow _dwVF)
        {
            if (!IsEdit)
                return;
            if (_dwVF == null)
                return;
            if (dwMaster == null)
                return;
            dwMaster["PART"] = _dwVF["VAFPART"];
            dwMaster["PVAR"] = _dwVF["VAFPVAR"];
            dwMaster["PFARBE"] = _dwVF["VAFFFARBE"];
            dwMaster["EKPWAKZ"] = ARTST;
            dwMaster["EKPART"] = Encoding.Default.GetBytes(_dwVF["VAFPART"].ToString());
            dwMaster["EKPVAR"] = Encoding.Default.GetBytes(_dwVF["VAFPVAR"].ToString());
            dwMaster["EKPFARBE"] = Encoding.Default.GetBytes(_dwVF["VAFFFARBE"].ToString());
            dwMaster["EKPBESCH"] = _dwVF["VAFBESCHICH"].ToString();
            dwMaster["EKPDESS"] = _dwVF["VAFDESS"].ToString();
            dwMaster["EKPCOL"] = _dwVF["VAFCOLORIT"].ToString();
            dwMaster["EKPGROE"] = _dwVF["VAFPGROE"].ToString();
            EXARTICLE = _dwVF["VAAARTST"].ToString() + (_dwVF["VAFPART"].ToString() + _dwVF["VAFPVAR"].ToString()).PadRight(22) + _dwVF["VAFFFARBE"].ToString();
            if (dwMain == null)
            { }
        }
        #endregion

        #region ==dwWS===========
        public static readonly DependencyProperty dwWSProperty = DependencyProperty.Register("dwWS",
        typeof(DataRow), typeof(UIEKP),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwWSChanged));
        public DataRow dwWS
        {
            get
            {
                return (DataRow)GetValue(dwWSProperty);
            }
            set
            {
                SetValue(dwWSProperty, value);
                

            }
        }
        private static void OndwWSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIEKP;
            if (src != null)
            {
                src.dwWS = (DataRow)e.NewValue;
                src.OnWSChg((DataRow)e.NewValue);
            }
        }
        
        #endregion

        #region ==KURS===========
        public static readonly DependencyProperty KURSProperty = DependencyProperty.Register("KURS",
        typeof(string), typeof(UIEKP),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnKURSChanged));
        public string KURS
        {
            get
            {
                return (string)GetValue(KURSProperty);
            }
            set
            {
                SetValue(KURSProperty, value);
                OnKURSChg(value);
            }
        }
        private static void OnKURSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIEKP;
            if (src != null)
            {
                src.KURS = (string)e.NewValue;
            }
        }
        public virtual void OnKURSChg(string value)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            if (value.Trim().Length == 0)
                return;
            foreach (DataRow _dwEKP in TBMain.Rows)
            {
                _dwEKP["EKPKURS"] = Helper.Right("0000000" + value, 7);
                UPDATEEKP(_dwEKP);
            }
        }
        #endregion

        #region ==WAEHRSL===========
        public static readonly DependencyProperty WAEHRSLProperty = DependencyProperty.Register("WAEHRSL",
        typeof(string), typeof(UIEKP),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWAEHRSLChanged));
        public string WAEHRSL
        {
            get
            {
                return (string)GetValue(WAEHRSLProperty);
            }
            set
            {
                SetValue(WAEHRSLProperty, value);
                OnWAEHRSLChg(value);
            }
        }
        private static void OnWAEHRSLChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIEKP;
            if (src != null)
            {
                src.WAEHRSL = (string)e.NewValue;
            }
        } 
         
        public virtual void OnWAEHRSLChg(string value)
        {
            if (!IsEdit)
                return;
            foreach (DataRow row in TBMain.Rows)
            {
                row["EKPWAEHRSL"] = WAEHRSL;
            } 
        }

        #endregion
        public TextBox TxtQty { get { return (TextBox)this.FindName("txtQty"); } }
        public TextBox TxtPrice { get { return (TextBox)this.FindName("txtPrice"); } }

        #region ==VKKRS===========
        public static readonly DependencyProperty VKKRSProperty = DependencyProperty.Register("VKKRS",
        typeof(string), typeof(UIEKP),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVKKRSChanged));
        public string VKKRS
        {
            get
            {
                return (string)GetValue(VKKRSProperty);
            }
            set
            {
                SetValue(VKKRSProperty, value);
            }
        }
        private static void OnVKKRSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIEKP;
            if (src != null)
            {
                src.VKKRS = (string)e.NewValue;
            }
        }
        #endregion

        #region ==VKLFD===========
        public static readonly DependencyProperty VKLFDProperty = DependencyProperty.Register("VKLFD",
        typeof(string), typeof(UIEKP),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVKLFDChanged));
        public string VKLFD
        {
            get
            {
                return (string)GetValue(VKLFDProperty);
            }
            set
            {
                SetValue(VKLFDProperty, value);
            }
        }
        private static void OnVKLFDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIEKP;
            if (src != null)
            {
                src.VKLFD = (string)e.NewValue;
            }
        }
        #endregion

        #region ==dwMEEH===========
        public static readonly DependencyProperty dwMEEHProperty = DependencyProperty.Register("dwMEEH",
        typeof(DataRow), typeof(UIEKP),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwMEEHChanged));
        public DataRow dwMEEH
        {
            get
            {
                return (DataRow)GetValue(dwMEEHProperty);
            }
            set
            {
                SetValue(dwMEEHProperty, value);
                if (dwMEEH == null)
                    return;
                if (IsEdit && dwMaster != null)
                {
                    dwMaster["EKPMEEH"] = dwMEEH["KEY"];
                    dwMaster["EKPMEEHGRP"] = dwMEEH["SDDATEN"].ToString().Substring(38, 1);
                    dwMaster["EKPMEEHFKT"] = dwMEEH["SDDATEN"].ToString().Substring(39, 12);
                    dwMaster["EKPPREHGRP"] = dwMaster["EKPMEEHGRP"];
                    dwMaster["EKPPREHFKT"] = dwMaster["EKPMEEHFKT"];
                }
            }
        }
        private static void OndwMEEHChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIEKP;
            DataRow row = (DataRow)e.NewValue;
            if (src != null)
            {
                src.dwMEEH = row;
            }
        }
        #endregion
    }
}
